package de.gameduell.framework.facebook.app.pages {
	import de.gameduell.framework.facebook.app.Controller;
	import de.gameduell.framework.facebook.app.PageManager;

	import de.gameduell.facebook.service.gen.UserProfile;
	import de.gameduell.framework.asset.AssetEvent;
	import de.gameduell.framework.asset.BitmapAsset;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.facebook.FBBaseController;
	import de.gameduell.framework.gui.util.Formatter;
	import de.gameduell.framework.gui.util.ToolTip;
	import de.gameduell.framework.gui.wrapper.SpriteWrapper;
	import de.gameduell.framework.util.Library;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.text.TextField;

	public class BuddyListItem extends SpriteWrapper{
		public static const ITEM_WIDTH:int = 87;
		protected var library:Library;
		protected var realName:TextField;
		protected var _buddy:UserProfile;
		protected var scoreText:TextField;
		protected var controller:Controller;
		protected var facebookPic:Sprite;
		protected var pageManager:PageManager;
		protected var toolTip:ToolTip;

		public function BuddyListItem(aBuddy:UserProfile){
			super(Library.getInstance().createSprite("buddyListItem"));
			controller = Controller.getInstance();
			library = Library.getInstance();
			pageManager = PageManager.getInstance();
			_buddy = aBuddy;
			buttonMode = false; // is not a button right now ...
			mouseChildren = false;
			realName = getChildByName("realname") as TextField;
			scoreText = getChildByName("experience") as TextField;
			var imageContainer:Sprite = getChildByName("imageContainer") as Sprite;
			facebookPic = imageContainer.getChildByName("image") as Sprite;
			
			realName.text = _buddy.firstName;
			Debug.trace("BuddyListItem::BuddyListItem "+_buddy.firstName);
			scoreText.text = Formatter.formatNumberEnglish(_buddy.highscore, ",");

			checkUser();
			loadPic();
		}
		
		
//		private function onClick(event:Event):void {
//			Controller.getInstance().registerScore("1000;true;completed;2");
//		}


		protected function checkUser():void{
			if (_buddy == controller.facebookData.user){
				var myColorTransform:ColorTransform = new ColorTransform();
				myColorTransform.color = 0x42741D;
				myColorTransform = new ColorTransform(0.89,0.93,0.85);
				mouseEnabled = true;
				buttonMode = true;
				getChildByName("bg").transform.colorTransform = myColorTransform;
				controller.addEventListener(FBBaseController.NEW_HIGHSCORE, updateScoreField);
				toolTip = new ToolTip("tooltip", "buddylistItem_tooltip", sprite, true);
				sprite.addEventListener(MouseEvent.CLICK, onShowMyPyramid);
			} else {
				getChildByName("bg").transform.colorTransform = new ColorTransform();
				mouseEnabled = true;
				buttonMode = true;
				sprite.addEventListener(MouseEvent.CLICK, onShowAFriendsPyramid);
				toolTip = new ToolTip("tooltip", "friendItem_tooltip", sprite, true);
			}
		}
		
		private function onShowMyPyramid(event:MouseEvent):void {
			if(!controller.gameIsRunning){
				if(pageManager.currentPage.id != PageManager.START_PAGE && pageManager.currentPage.isContent){
					pageManager.show(PageManager.START_PAGE);
					// in case it's enabled enabled
					if(controller.featureList.isEnabled(Controller.JACKPOT_FEATURE_ID)) {
						pageManager.show(PageManager.JACKPOT);
					}
				}
			}
		}

		protected function onShowAFriendsPyramid(event:Event):void {
			if(!controller.gameIsRunning){
				controller.foreignUserId = _buddy.uid;
				if(pageManager.currentPage.id != PageManager.FOREIGN_PYRAMID){
					pageManager.show(PageManager.FOREIGN_PYRAMID);
					// in case it's enabled enabled
					// pageManager.hide(PageManager.JACKPOT);
				}else{
					ForeignPyramidPage(pageManager.currentPage).updatePyramidUser();
				}
			}
		}

		private function updateScoreField(event:Event):void {
			scoreText.text = Formatter.formatNumberEnglish(String(controller.gameData.pyramid.totalScore), ",");
		}

		public function get buddy():UserProfile {
			return _buddy;
		}
		
		public function loadPic():void{
			if(checkPicUrl()){
				var picUrl:String = buddy.image;
				var asset:BitmapAsset = new BitmapAsset("pic", picUrl);
				asset.addEventListener(AssetEvent.COMPLETE, onPicLoaded);
				asset.addEventListener(AssetEvent.LOAD_ERROR, onPicLoadError);
				asset.load();
			}else{
				onPicLoadError();
			}
		}
		
		private function onPicLoadError(event:Event = null):void {
			try {
				var bitmapData:BitmapData = library.createBitmapData("fb_pic_square_fallback", 50, 50);
				facebookPic.addChild((new Bitmap(bitmapData)));
			}catch(e:Error) {
				Debug.trace("BuddyListItem::onPicLoadError: default_fb_avatar not in lib ... ", Debug.WARNING);
			}
		}
		
		private function checkPicUrl():Boolean {
			if(buddy != null) {
				if(buddy.image != null) {
					if(buddy.image != "") {
						return true;
					} 
				} 
			} 
			return false;
		}
		
		private function onPicLoaded(event:AssetEvent):void {
			try{
				facebookPic.addChild(BitmapAsset(event.asset).bitmap);
			}catch(error:Error) {
				facebookPic.addChild(library.createBitmap("fb_pic_square_fallback"));
			}
		}
	}
}
